package com.fry.expandablelistviewexdemo;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.fry.expandablelistviewexdemo.ExpandableListViewEx.PinnedHeaderAdapter;

public class SampleAdapter 
	extends BaseExpandableListAdapter
	implements PinnedHeaderAdapter {

	private ArrayList<ArrayList<String>> mData;
	
	private Context mContext;
	private LayoutInflater mInflater;
	public SampleAdapter(Context context) {
		mContext = context;
		mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		initData();
	}
	
	private void initData() {
		mData = new ArrayList<ArrayList<String>>();
		for (int i = 0; i < 40; i++) {
			ArrayList<String> group = new ArrayList<String>();
			for (int j = 0; j < 3; j++) {
				group.add("item" + j);
			}
			mData.add(group);
		}
	}
	
	@Override
	public int getGroupCount() {
		return mData.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return mData.get(groupPosition).size();
	}

	@Override
	public String getGroup(int groupPosition) {
		return "group" + groupPosition;
	}

	@Override
	public String getChild(int groupPosition, int childPosition) {
		return mData.get(groupPosition).get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		View v;
		
		if (convertView == null) {
			v = mInflater.inflate(R.layout.list_item, parent, false);
		} else {
			v = convertView;
		}
		TextView text = (TextView)v.findViewById(R.id.name);
		text.setText(getGroup(groupPosition));
		return v;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		View v;
		
		if (convertView == null) {
			v = mInflater.inflate(R.layout.list_item, parent, false);
		} else {
			v = convertView;
		}
		TextView text = (TextView)v.findViewById(R.id.name);
		text.setText(getChild(groupPosition, childPosition));
		return v;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	@Override
	public void configurePinnedHeader(View header, long packedPosition) {
		int type = ExpandableListViewEx.getPackedPositionType(packedPosition);
		
	}

}
